package com.lineage.server.serverpackets;

import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;

public class S_NPCPack extends ServerBasePacket {
  private static final int STATUS_POISON = 1;
  
  private static final int STATUS_PC = 4;
  
  private byte[] _byte;
  
  public S_NPCPack(L1NpcInstance npc, L1PcInstance pc) {
    this._byte = null;
    writeC(87);
    writeH(npc.getX());
    writeH(npc.getY());
    writeD(npc.getId());
    if (npc.getTempCharGfx() == 0) {
      writeH(npc.getGfxId());
    } else {
      writeH(npc.getTempCharGfx());
    } 
    if (npc.getNpcTemplate().is_doppel() && npc.getGfxId() != 31) {
      writeC(4);
    } else {
      writeC(npc.getStatus());
    } 
    writeC(npc.getHeading());
    writeC(npc.getChaLightSize());
    writeC(npc.getMoveSpeed());
    writeD((int)npc.getExp());
    writeH(npc.getTempLawful());
    String levelname = npc.getNameId();
    if ((pc.getLevel() - npc.getLevel() >= 0 && pc.getLevel() - npc.getLevel() <= 5) || (
      npc.getLevel() - pc.getLevel() >= 0 && npc.getLevel() - pc.getLevel() <= 5)) {
      levelname = "\\f2" + npc.getNameId();
    } else if (npc.getLevel() - pc.getLevel() >= 30) {
      levelname = "\\f3" + npc.getNameId();
    } else if (npc.getLevel() - pc.getLevel() >= 20) {
      levelname = "\\fC" + npc.getNameId();
    } else if (npc.getLevel() - pc.getLevel() >= 11) {
      levelname = "\\f:" + npc.getNameId();
    } 
    writeS(levelname);
    writeS(npc.getTitle());
    int status = 0;
    if (npc.getPoison() != null && npc.getPoison().getEffectId() == 1)
      status |= 0x1; 
    if (npc.getNpcTemplate().is_doppel() && npc.getNpcTemplate().get_npcId() != 81069)
      status |= 0x4; 
    if (npc.getNpcTemplate().get_npcId() == 90024)
      status |= 0x1; 
    writeC(status);
    writeD(0);
    writeS(null);
    writeS(null);
    writeC(0);
    writeC(255);
    writeC(0);
    writeC(0);
    writeC(0);
    writeC(255);
    writeC(255);
  }
  
  public S_NPCPack(L1NpcInstance npc) {
    this._byte = null;
    writeC(87);
    writeH(npc.getX());
    writeH(npc.getY());
    writeD(npc.getId());
    if (npc.getTempCharGfx() == 0) {
      writeH(npc.getGfxId());
    } else {
      writeH(npc.getTempCharGfx());
    } 
    if (npc.getNpcTemplate().is_doppel() && npc.getGfxId() != 31) {
      writeC(4);
    } else {
      writeC(npc.getStatus());
    } 
    writeC(npc.getHeading());
    writeC(npc.getChaLightSize());
    writeC(npc.getMoveSpeed());
    writeD((int)npc.getExp());
    writeH(npc.getTempLawful());
    writeS(npc.getNameId());
    writeS(npc.getTitle());
    int status = 0;
    if (npc.getPoison() != null && npc.getPoison().getEffectId() == 1)
      status |= 0x1; 
    if (npc.getNpcTemplate().is_doppel() && npc.getNpcTemplate().get_npcId() != 81069)
      status |= 0x4; 
    if (npc.getNpcTemplate().get_npcId() == 90024)
      status |= 0x1; 
    writeC(status);
    writeD(0);
    writeS(null);
    writeS(null);
    writeC(0);
    writeC(255);
    writeC(0);
    writeC(0);
    writeC(0);
    writeC(255);
    writeC(255);
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
